package com.ahies.utils; /**
 * @Description
 * @Date 2019/10/18 11:35
 * @Author zsj
 */

import org.springframework.context.ApplicationContext;

import java.util.Objects;

/**
 * Spring获取上下文或者Bean工具类
 *
 * @author WuZhiWei
 * @since 2018-04-09
 */

public class SpringContextUtil {

    private static ApplicationContext applicationContext;

    //获取上下文
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //设置上下文
    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil.applicationContext = applicationContext;
    }

    //通过名字获取上下文中的bean
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    //通过类型获取上下文中的bean
    public static Object getBean(Class<?> requiredType) {
        if (Objects.nonNull(applicationContext)) {
            return applicationContext.getBean(requiredType);
        }
        return null;

    }
}